﻿using MySql.Data.MySqlClient;
using SeCompUFSCarWeb.Evento;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SeCompUFSCarWeb
{
    public partial class EmailsDeCorrecao : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            string strconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            MySqlConnection conn = new MySqlConnection(strconn);

            string query = "Select course_id, email, fullname from secomp.coursesignup left join secomp.aspnetusers on user_id = aspnetusers.id where course_id = 7";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            string htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Smashing.html"));

            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Smashing.html"));
                    htmlCode = htmlCode.Replace("#NOME", reader.GetString("fullname"));
                    Mail.Send(reader.GetString("email"), htmlCode, "Cancelamento do Minicurso Smashing the Stack for Fun");
                }
            }
            catch (Exception)
            {
                Response.Write("Deu merda");
            }
            finally
            {
                conn.Close();
            }

            Response.Write("Sucesso");


        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            string strconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            MySqlConnection conn = new MySqlConnection(strconn);

            string query = "Select course_id, email, fullname from secomp.coursesignup left join secomp.aspnetusers on user_id = aspnetusers.id where course_id = 8";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            string htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/4g.html"));

            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/4g.html"));
                    htmlCode = htmlCode.Replace("#NOME", reader.GetString("fullname"));
                    Mail.Send(reader.GetString("email"), htmlCode, "Cancelamento do Minicurso IMPLANTAÇÃO DAS REDES 4G NO BRASIL");
                }
            }
            catch (Exception)
            {
                Response.Write("Deu merda");
            }
            finally
            {
                conn.Close();
            }

            Response.Write("Sucesso");

        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            string strconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            MySqlConnection conn = new MySqlConnection(strconn);

            string query = "SELECT ASPNETUSERS.FULLNAME, ASPNETUSERS.EMAIL FROM SECOMP.ASPNETUSERS, SECOMP.SIGNUP WHERE aspnetusers.ID = signup.user_id ORDER BY FULLNAME;";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            string htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Novidades.html"));

            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Novidades.html"));
                    htmlCode = htmlCode.Replace("#NOME", reader.GetString("fullname"));
                    Mail.Send(reader.GetString("email"), htmlCode, "V SeComp - Fique ligado na nossa programação");
                }
            }
            catch (Exception)
            {
                Response.Write("Deu merda");
            }
            finally
            {
                conn.Close();
            }

            Response.Write("Sucesso");
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            string strconn = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            MySqlConnection conn = new MySqlConnection(strconn);

            string query = "SELECT SIGNUP.ID, SIGNUP.PACK, ASPNETUSERS.FULLNAME, ASPNETUSERS.EMAIL FROM SECOMP.SIGNUP, SECOMP.ASPNETUSERS WHERE SIGNUP.USER_ID = ASPNETUSERS.ID AND SIGNUP.PAYMENT = 1 AND SIGNUP.BARBECUE = 1 ORDER BY FULLNAME";
            MySqlCommand cmd = new MySqlCommand(query, conn);

            string htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Churrasco.html"));

            try
            {
                conn.Open();
                MySqlDataReader reader = cmd.ExecuteReader();

                while (reader.Read())
                {
                    htmlCode = System.IO.File.ReadAllText(Server.MapPath("~/Emails/Churrasco.html"));
                    htmlCode = htmlCode.Replace("#NOME", reader.GetString("fullname"));
                    Mail.Send(reader.GetString("email"), htmlCode, "Cancelamento do Churrasco de Integração");
                }
            }
            catch (Exception)
            {
                Response.Write("Deu merda");
            }
            finally
            {
                conn.Close();
            }

            Response.Write("Sucesso");
        }
    }
}